home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d20 / doorskl3.arc / DOORSKL3.C < prev    next >
C/C++ Source or Header  |  1992-01-15  |  11KB  |  422 lines

  1. /**********************************************/
  2. /*                                              */
  3. /* XBBS Door Skeleton -- TC 2.0/MSC 6.0a code */
  4. /* Copyright (c) 1990/91 by M. Kimes          */
  5. /* All Rights Reserved                        */
  6. /* May be freely used for >>>FREE<<< programs */
  7. /* as long as you don't try to save any souls */
  8. /* (nasty habit)                              */
  9. /*                                            */
  10. /**********************************************/
  11.  
  12. /* you can omit this module if you don't want the file reader readtext()...
  13.    has the same effect as including it but #defining NOREADFILE */
  14.  
  15. /**************************************************************************
  16.   Miscellaneous notes:
  17.     #define NOREADFILE and the file reader routine (readtext()) will NOT
  18.             be compiled (same as not including this module)
  19. ***************************************************************************/
  20.  
  21. #include "doorskel.h"   /* XBBS Door include file */
  22.  
  23.  
  24.  
  25.  
  26. #ifndef NOREADFILE
  27.  
  28. char _fastcall readtext (char *s,char paged) {  /* Displays a text file
  29.                                                 This thing's got all
  30.                                                 sorts of fancy stuff
  31.                                                 in it if you set paged = 1
  32.                                                 Otherwise it does a pretty
  33.                                                 dumb dump (ok for ANSI
  34.                                                 files).  s is the filename
  35.                                                 to read.  You can't do much
  36.                                                 better with an online file
  37.                                                 reader than this; it lets
  38.                                                 you page forward and backward
  39.                                                 through the text, search for
  40.                                                 text strings, jump to home or
  41.                                                 end of file, etc.  All the
  42.                                                 modern conveniences. */
  43.  
  44.     long place2;
  45.     long place;
  46.     struct stat filestat;
  47.     char dostring[181];
  48.     char eeof=0;
  49.     word page;
  50.     char a;
  51.     long *pos;
  52.     word scratch;
  53.     FILE *fp;
  54.     register int x;
  55.     char prev=0;
  56.     char *here;
  57.     char *p;
  58.     char *tempo;
  59.     char temp='N';
  60.     char lastpage=0;
  61.     char searchstring[41];
  62.     char holdstring[41];
  63.     char teststring[81];
  64.     char findend=0;
  65.     char printanyway=0;
  66.     char linelen;
  67.     word lines=1;
  68.  
  69.  
  70.     *searchstring=0;
  71.     *holdstring=0;
  72.  
  73.   if (stat(s,&filestat)) return 1;
  74.   if (!filestat.st_size) return 1;
  75.   if (!(fp=fopen(s,"rt"))) return 1;
  76.   cls();
  77.   printg("\x1b[0;1;33m");
  78.   page=0;
  79.   place2=ftell(fp);
  80.   if (!paged) {
  81.       printm("\r\n");
  82.       while (!feof(fp) && !ferror(fp)) {
  83.         if (!fgets(dostring,181,fp)) break;
  84.         if(ftell(fp)<=place2) {
  85.             while(fgetc(fp)=='\n');  /* Guard against Unix files & TC bug */
  86.             continue;
  87.         }
  88.         place2=ftell(fp);
  89.         printm(dostring);
  90.         if(numlines) {
  91.             lines++;
  92.             if(lines > numlines+1) {
  93.                 lines=1;
  94.                 printm("More? (Y/n) ");
  95.                 fossil(FLUSHOUT,0);
  96.                 fossil(PURGEIN,0);
  97.                 linelen=*genin(1,0,1,1,YESNOM);
  98.                 for (x=0;x<12;x++) printm(BACKSPACE);
  99.                 if (linelen == 'N') break;
  100.             }
  101.         }
  102.         a = (char)toupper(inkey());
  103.         if (a=='S' || a==' ') break;
  104.         if (a=='P') my_sleep(5);
  105.       }
  106.       fclose(fp);
  107.       if (numlines) if (lines > numlines-11) hitreturn();
  108.       return 0;
  109.   }
  110.  
  111. /*  Can be used to invoke an external reader like LIST
  112.   if (!baud && *reader) {
  113.     fclose(fp);
  114.     sprintf(dostring,"%s %s",reader,s);
  115.     system(dostring);
  116.     fputs("\x1b[2J\x1b[0m",stdout);
  117.     print_stat();
  118.     return 0;
  119.   }
  120. */
  121.  
  122.   p=strchr(s,':');            /* Strip path off filename for display */
  123.   here=strrchr(s,'\\');
  124.   if(!p && !here) here=s;
  125.   else if (!here) here=&p[1];
  126.   else if (p) here= (p<here) ? &here[1] : &p[1];
  127.   else here++;
  128.  
  129.   pos=(long *)malloc((word)(sizeof(long) * 1024));
  130.   if(!pos) {
  131.     fclose(fp);
  132.     printm("\r\nOut of memory\r\n");
  133.     exit(4);
  134.   }
  135.  
  136.   printfm(" -=-=Reading: %s=-=-  Page #%u\r\n",here,page+1);
  137.  
  138.   printg("\x1b[0;1;32m");
  139.   if(skip_blanks(fp)) goto Abort;
  140.   place2=pos[0]=ftell(fp);
  141.   lines=1;
  142.   while (!feof(fp) && !ferror(fp)) {
  143.     if ((*searchstring && !printanyway) || findend) goto Searcher;
  144. Domore:
  145.     if (!fgets(dostring,181,fp)) {
  146.         eeof++;
  147.         goto EEEOF;
  148.     }
  149.     if(ftell(fp) <= place2) {
  150.         while(fgetc(fp) == '\n');  /* Guard against Unix files & TC bug */
  151.         continue;
  152.     }
  153.     place2 = ftell(fp);
  154.     stripcr(dostring);
  155.     while (p=strchr(dostring,'\xc')) memmove(p,&p[1],strlen(&p[1])+1);
  156.     while (p=strchr(dostring,'\x1b')) memmove(p,&p[1],strlen(&p[1])+1);
  157.     if (!*dostring) {
  158.         if (prev) {
  159.             prev = 0;
  160.             eeof = skip_blanks(fp);
  161.             if (eeof) goto EEEOF;
  162.             else goto Domore;
  163.         }
  164.         else prev++;
  165.     }
  166.     else prev=0;
  167.     if (*searchstring) {
  168.         strcpy(teststring,dostring);
  169.         strupr(teststring);
  170.         if (strstr(teststring,searchstring)) {
  171.             printg("\x1b[0;1;37m");
  172.             printm(dostring);
  173.             printg("\x1b[0;1;32m");
  174.             lines++;
  175.         }
  176.         else {
  177.             printm(dostring);
  178.             lines++;
  179.         }
  180.     }
  181.     else {
  182.         printm(dostring);
  183.         lines++;
  184.     }
  185.     scratch=strlen(dostring);
  186.     p=dostring;
  187.     while(tempo=strchr(p,'\t')) {
  188.         p=tempo+1;
  189.         scratch+=7;
  190.     }
  191.     if (scratch < 79) {
  192.         printm("\r\n");
  193.         scratch++;
  194.     }
  195.     if (scratch>width) {
  196.         lines++;
  197.     }
  198.     a = (char)toupper(inkey());
  199.     if (a=='S' || a==' ') break;
  200.     if (a=='P' || a=='-') if (page) goto SkipThis;
  201.     if (a=='M') goto Direct;
  202. Searcher:
  203.     if (lastpage) goto EEEOF;
  204.     if ((a=='N') || (*searchstring) || (findend)) {
  205.       if (findend) {
  206.         if (++page>1024) page=1;
  207.         pos[page]=ftell(fp);
  208.       }
  209.       place2=place=ftell(fp);
  210. Domore1:
  211.       while (!feof(fp) && !ferror(fp)) {
  212. Domore2:
  213.         if (!fgets(dostring,181,fp)) {
  214.             eeof++;
  215.             break;
  216.         }
  217.         if(ftell(fp)<=place2) {
  218.             while(fgetc(fp)=='\n');  /* Guard against Unix files & TC bug */
  219.             continue;
  220.         }
  221.         place2=ftell(fp);
  222.         while (p=strchr(dostring,'\xc')) memmove(p,&p[1],strlen(&p[1])+1);
  223.         while (p=strchr(dostring,'\x1b')) memmove(p,&p[1],strlen(&p[1])+1);
  224.         if (!strcmp(dostring,"\n")) {
  225.             if (prev) {
  226.                 prev=0;
  227.                 eeof=skip_blanks(fp);
  228.                 if (eeof) break;
  229.                 else goto Domore2;
  230.             }
  231.             else prev++;
  232.         }
  233.         else prev=0;
  234.         if (*searchstring) {
  235.             if (strstr(strupr(dostring),searchstring)) {
  236.                 printanyway++;
  237.                 break;
  238.             }
  239.         }
  240.         lines++;
  241.         scratch=strlen(dostring);
  242.         p=dostring;
  243.         while(tempo=strchr(p,'\t')) {
  244.             p=tempo+1;
  245.             scratch+=3;
  246.         }
  247.         if (scratch<79) {
  248.             scratch++;
  249.         }
  250.         if (scratch>width) lines++;
  251.         if (numlines) if ((lines >= numlines-2) || (feof(fp)) || (eeof)) break;
  252.       }
  253.       fossil(PURGEIN,0);
  254.       eeof=skip_blanks(fp);
  255.       if (feof(fp) || eeof || printanyway) {
  256.         if (*searchstring && !printanyway) {
  257.             printm("\r\nNot found...\r\n");
  258.             goto EEEOF;
  259.         }
  260.         fseek(fp,place,SEEK_SET);
  261.         clearerr(fp);
  262.         cls();
  263.         printg("\x1b[0;1;33m");
  264.         printfm(" -=-=Reading: %s=-=-  Page #%u\r\n",here,page+1);
  265.         printg("\x1b[0;1;32m");
  266.         findend=prev=eeof=0;
  267.         lastpage = 1;
  268.         lines = 1;
  269.         goto Domore;
  270.       }
  271.       if ((*searchstring || findend) && !eeof) {
  272.         if (++page>1024) page=1;
  273.         pos[page]=place=place2=ftell(fp);
  274.         lines=1;
  275.         goto Domore1;
  276.       }
  277.     }
  278. EEEOF:
  279.     if (numlines) if ((lines >= numlines-2) || (feof(fp)) || (eeof)) {
  280. Direct:
  281.         linelen=0;
  282.         prev=0;
  283.         eeof=skip_blanks(fp);
  284.         if (feof(fp) || eeof) {
  285.             printg("\x1b[0;1;31m");
  286.             printm(" (END)");
  287.             linelen+=6;
  288.             printg("\x1b[0;1;33m");
  289.             temp='S';
  290.         }
  291.         else {
  292.             if (a=='M' || a=='N') goto SkipThis;
  293.             temp='N';
  294.             printg("\x1b[0;1;33m");
  295.             printm(" [F]ind [E]nd [N]ext [M]ore");
  296.             linelen+=27;
  297.         }
  298.         if (page) {
  299.             printm(" [H]ome [P]rior");
  300.             linelen+=15;
  301.         }
  302.         linelen+=sprintf(dostring," [O]ver [S]top: [%c] ",temp);
  303.         printm(dostring);
  304.         fossil(FLUSHOUT,0);
  305.         pos[page+1]=ftell(fp);
  306. Getitagain:
  307.         fossil(PURGEIN,0);
  308.         a=*genin(1,0,1,1,ALLL);
  309. SkipThis:
  310.         findend=0;
  311.         lastpage=0;
  312.         printanyway=0;
  313.         *searchstring=0;
  314.         switch (a) {
  315.             case 'S':   goto Abort;
  316.             case 'H':   if (!page) {
  317.                             printm(BACKSPACE);
  318.                             printm("\x7");
  319.                             goto Getitagain;
  320.                         }
  321.                         page=0;
  322.                         fseek(fp,0,SEEK_SET);
  323.                         break;
  324.             case 'E':   if (feof(fp) || eeof) {
  325.                             printm(BACKSPACE);
  326.                             printm("\x7");
  327.                             goto Getitagain;
  328.                         }
  329.                         findend++;
  330.                         break;
  331.             case 'F':   if (feof(fp) || eeof) {
  332.                             printm(BACKSPACE);
  333.                             printm("\x7");
  334.                             goto Getitagain;
  335.                         }
  336.                         printg("\x1b[0;1;35m");
  337.                         printfm("\r\n Search string: [%s]\r\n  -> ",holdstring);
  338.                         strcpy(searchstring,genin(40,0,1,0,ALLL));
  339.                         if (!*searchstring) {
  340.                             if (*holdstring) {
  341.                                 strcpy(searchstring,holdstring);
  342.                                 if (++page>1024) page=1;
  343.                                 pos[page]=ftell(fp);
  344.                             }
  345.                         }
  346.                         else strcpy(holdstring,searchstring);
  347.                         fseek(fp,pos[page],SEEK_SET);
  348.                         eeof=skip_blanks(fp);
  349.                         break;
  350.             case  0:    if (feof(fp) || eeof) goto Abort;
  351.             case 'N':   if (feof(fp) || eeof) {
  352.                             printm(BACKSPACE);
  353.                             printm("\x7");
  354.                             goto Getitagain;
  355.                         }
  356.                         a=0;
  357.                         if (++page>1024) page=1;
  358.                         pos[page]=ftell(fp);
  359.                         break;
  360.             case 'O':   fseek(fp,pos[page],SEEK_SET);
  361.                         eeof=skip_blanks(fp);
  362.                         break;
  363.             case 'P':
  364.             case '-':   if (page) {
  365.                             fseek(fp,pos[--page],SEEK_SET);
  366.                             eeof=skip_blanks(fp);
  367.                             break;
  368.                         }
  369.                         printm(BACKSPACE);
  370.                         printm("\x7");
  371.                         goto Getitagain;
  372.             case 'M':   if (feof(fp) || eeof) {
  373.                             printm("\x7");
  374.                             printm(BACKSPACE);
  375.                             goto Getitagain;
  376.                         }
  377.                         for (x=0;x < linelen;x++) printm(BACKSPACE);
  378.                         printg("\x1b[0;1;32m");
  379.                         if (numlines) lines=numlines-3;
  380.                         goto Domore;
  381.             default:    printm(BACKSPACE);
  382.                         goto Getitagain;
  383.         }
  384.         place2=ftell(fp);
  385.         eeof=0;
  386.         cls();
  387.         printg("\x1b[0;1;33m");
  388.         if (findend || *searchstring) strcpy(teststring,"Scanning");
  389.         else strcpy(teststring,"Reading");
  390.         printfm(" -=-=%s: %s=-=-  Page #%u\r\n",teststring,here,page+1);
  391.         printg("\x1b[0;1;32m");
  392.         lines=1;
  393.       }
  394.   }
  395. Abort:
  396.   if(pos) free(pos);
  397.   fclose(fp);
  398.   return 0;
  399.  
  400. }
  401.  
  402.  
  403.  
  404. char _fastcall skip_blanks (FILE *fp) {  /* Skips blank lines, what else? */
  405.  
  406.   ulong pos;
  407.   char temp[81];
  408.   char eeof=0;
  409.  
  410.  
  411.   do {
  412.     pos=ftell(fp);
  413.     if (!fgets(temp,81,fp)) eeof++;
  414.     stripcr(temp);
  415.   } while ((!eeof) && (!feof(fp)) && (!*temp));
  416.   if (!eeof && !feof(fp)) fseek(fp,pos,SEEK_SET);
  417.   return eeof;
  418.  
  419. }
  420.  
  421. #endif
  422.